// https://leetcode.cn/problems/unique-paths-ii/


#include <iostream>

using namespace std;

class Solution {
public:
    int uniquePathsWithObstacles(vector <vector<int>> &obstacleGrid) {
        int rows = obstacleGrid.size();
        int cols = obstacleGrid[0].size();
        int dp[rows][cols];
        if(obstacleGrid[rows-1][cols-1] == 1) return 0;
        int flag = 1;
        for (int i = 0; i < rows; i++) {
            dp[i][0] = obstacleGrid[i][0] == 0 && flag == 1 ? 1 : 0;
            if(dp[i][0] == 0) flag = 0;
        }
        flag = 1;
        for (int i = 0; i < cols; i++) {
            dp[0][i] = obstacleGrid[0][i] == 0 && flag == 1 ? 1 : 0;
            if(dp[0][i] == 0) flag = 0;
        }
        for (int i = 1; i < rows; i++) {
            for (int j = 1; j < cols; j++) {
                if (obstacleGrid[i][j] == 1) {
                    dp[i][j] = 0;
                } else {
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
        }
        return dp[rows - 1][cols - 1];
    }

};

int main() {
    Solution so;
    int m = 3;
    int n = 2;
    int a = so.uniquePaths(m, n);
    cout << a << endl;
    return 0;
}